PFont font;
PImage fredaSeal;

int stateMachine = 0;

int range = 51; // range of 2 random number for GCD
int int_a, int_b; // gcd(int_a, int_b);

// showing control
int x_pos, y_pos; // where to show
final int X_POS_INIT = 50;
final int Y_POS_INIT = 250;
final int X_INC = 150;
final int Y_INC = 50;

String verMsg = "GCD (Greatest Common Divisor) calculation show V3.4";
String verMsgDate = "Chuyi Li. Sep 14 2024";

void setup() {
  size(1520, 880);
  smooth();
  frameRate(20);
  font = loadFont("BradleyHandITC-120.vlw");
  textFont(font);
  println(verMsg + " " + verMsgDate);
  fill(#F5F09C);
  fredaSeal = loadImage("freda_seal.png");
}

void drawBasicGUI() {
  background(#458B3F);
  // ============= //
  fill(#F5F09C);
  textSize(40);
  text(verMsg, 5, 40);
  text(verMsgDate, 5, 80);

  textSize(50);
  text("Range", 1139, 60);
  image(fredaSeal, 1250, 620);
  drawButtons();
}

int b, s, q, r;

void draw() {
  // ====================== //
  switch(stateMachine) {
  case 0:
    {
      drawBasicGUI();
      break;
    }
  case 1:
    {
      textSize(100);
      text(int_a, 160, 156);
      text(int_b, 630, 156);
      println("\nb   s   q   r");
      stateMachine = 2;
      break;
    }
  case 2:
    {
      if (gcd_b.isNotEmpty()) { // all 4 stacks are same length
        b = gcd_b.out();
        s = gcd_s.out();
        q = gcd_q.out();
        r = gcd_r.out();
        println(b + " " + s + " " + q + " " + r);

        textSize(50);
        text(b, x_pos, y_pos);
        x_pos += X_INC;

        text("/", x_pos, y_pos);
        x_pos += X_INC;

        text(s, x_pos, y_pos);
        x_pos += X_INC;

        text("=", x_pos, y_pos);
        x_pos += X_INC;

        text(q, x_pos, y_pos);
        x_pos += X_INC;

        text("...", x_pos, y_pos);
        x_pos += X_INC;

        text(r, x_pos, y_pos); // 1 line ends

        x_pos =  X_POS_INIT; // return
        y_pos += Y_INC; // new line

        delay(450); // just a simple blocked delay. I don't plan to use non-blocked delay
      } else {
        stateMachine = 3;
      }
      break;
    }
  case 3:
    {
      text("Greastest Common Divisor = " + s, x_pos, y_pos + 30); // the 's' (small number) in last round is the GCD
      println("GCD = " + s);
      stateMachine = 4;
      break;
    }
  case 4:
    {
      enableAllButtons();
      btnGo.unfade();
      btnGo.paint();
      break;
    }
  default:
    {
      stateMachine = 0;
    }
  }
}

// ====================== //

void mouseClicked() {
  checkMouseClick(mouseX, mouseY);
  // println(mouseX + " " + mouseY); // for debug
}

// ====================== //

void drawButtons() {
  for (cAllButton e : allButtons) {
    e.paint();
  }
}

// ====================== //

void checkMouseClick(int mx, int my) {
  for (cAllButton e : allButtons) {
    e.checkMouseClick(mx, my);
  }
}
